<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">{{t('sys.configuration.addTemplate')}}</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: t('sys.configuration.edit'),
                tooltip: t('sys.configuration.edit'),
                onClick: handleCreate.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, computed, watch } from 'vue';
  import { useModal } from '@/components/Modal';
  import { message } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getvmAPI } from '@/api/demo/mvm';
  import { PageWrapper } from '@/components/Page';
  import { columns } from './account.data';
  import { useGo } from '@/hooks/web/usePage';
  import AccountModal from './AccountModal.vue';
  import { useWebSocketStore } from '@/store/modules/websocket';

  const webSocketStore = useWebSocketStore();
  import { useI18n } from "@/hooks/web/useI18n";

  const { t } = useI18n();
  defineOptions({ name: 'AccountManagement' });
  const total = ref(0);

  const tableTitle = computed(() => `${t('sys.configuration.commentTemplate')} ${t('sys.configuration.common')} ${total.value} ${t('sys.configuration.strip')}`);
  const go = useGo();
  const searchInfo = reactive<Recordable>({});
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, updateTableDataRecord }] = useTable({
    title: tableTitle,
    // api: async (params) => {
    //   const result = (await getvmAPI(params)) as { items: any[]; total: number };
    //   total.value = result.total;
    //   return result;
    // },
    rowKey: 'id',
    columns,
    useSearchForm: false,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 20,
      title: t('sys.configuration.action'),
      dataIndex: 'action',
    },
  });

  // async function handleDelete(record: Recordable) {
  //   await dataDeleteAPI({ id: record.id });
  //   message.success('操作完成');
  //   reload();
  // }

  function handleView(record: Recordable) {
    go('/system/account_detail/' + record.id);
  }

  function handleCreate(record) {
    openModal(true, {
      record,
      isUpdate: false,
    });
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      const result = updateTableDataRecord(values.id, values);
      console.log(result);
    } else {
      reload();
    }
  }

  // 监听 webSocketStore 中的 state 变化
  watch(
    () => webSocketStore.$state,
    (newState, oldState) => {
      console.log('webSocketStore state changed:', newState, oldState);
      // 在这里可以执行一些操作，例如更新表格数据
    },
    { deep: true },
  );
</script>
